package com.situ.mvc.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.situ.mvc.common.ServerResponse;
import com.situ.mvc.entity.Student;
import com.situ.mvc.entity.StudentTeacherBean;

@Controller
@RequestMapping(value="/student")
public class StudentController {
	
	@RequestMapping(value = "/getStudentInsertPage")
	public String getStudentInsertPage() {
		// request.getRequestDispather("/WEB-INF/student_info.jsp").forward(req,resp);
		// return "/WEB-INF/jsp/student_insert.jsp";
		return "student_insert";
	}
	
	@RequestMapping(value="/insert")
	public ModelAndView insert(Student student) {
		System.out.println("StudentController.insert()");
		
		System.out.println(student);
		// request.setAttribute("student", student);
		ModelAndView modelAndView = new ModelAndView();
		// 将数据放到域对象中，就是放到request这个域对象
		modelAndView.addObject("student", student);
		// request.getRequestDispather("/student_info.jsp").forward(req,resp);
		// modelAndView.setViewName("/WEB-INF/jsp/student_info.jsp");
		modelAndView.setViewName("student_info");
		return modelAndView;
		
	}
	@RequestMapping(value="/insert2", method = RequestMethod.POST)
	public String insert(Student student, Model model) {
		System.out.println("StudentController.insert()");
		
		System.out.println(student);
		System.out.println(student);
		model.addAttribute("student",student);
		return "student_info";
	}
	
	@RequestMapping(value = "/insert3", method=RequestMethod.POST)
	public void insert3(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("StudentController.insert3()");
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		String gender = request.getParameter("gender");
		Student student = new Student(name, Integer.parseInt(age), gender);
		
		HttpSession session = request.getSession();
		request.setAttribute("student", student);
		session.setAttribute("student", student);
		request.getRequestDispatcher("/WEB-INF/student_info.jsp").forward(request, response);
	}
	
	@RequestMapping("/insertStudentAndTeacher")
	public String insertStudentAndTeacher(StudentTeacherBean bean) {
		System.out.println(bean.getStudent());
		System.out.println(bean.getTeacher());
		
		return "student_info";
	}
	// /SpringMVC1907/student/deleteById.action?id=2
	@RequestMapping(value = "/deleteById")
	public String deleteById(Integer id) {
		System.out.println("StudentController.deleteById()");
		System.out.println(id);
		
		// 转发到某个jsp界面
				//return "student_info";
				
		// 转发到/student/pageList.action
		//return "forward:/student/pageList.action"
		
		// 重定向
		return "redirect : /student/pageList.action";
	}
	@RequestMapping("/pageList")
	@ResponseBody
	public List<Student> pageList(@RequestParam(required = true)Integer pageNo,
			@RequestParam(value = "ps",defaultValue = "3")Integer pageSize){
		System.out.println("StudentController.pageList()---------");
		System.out.println("pageNo" + pageNo);
		System.out.println("pageSize" + pageSize);
		
		List<Student>list = new ArrayList<Student>();
		Student student1 = new Student("张三",22,"男");
		Student student2 = new Student("张三2",22,"男");
		Student student3 = new Student("张三3",22,"男");
		Student student4 = new Student("张三4",22,"男");
		list.add(student1);
		list.add(student2);
		list.add(student3);
		list.add(student4);
		return list;
	}
	@RequestMapping("/pageObj")
	@ResponseBody
	public ServerResponse pageObj() {
System.out.println("StudentController.pagList()");
		
		List<Student>list = new ArrayList<Student>();
		Student student1 = new Student("张三",22,"男");
		Student student2 = new Student("张三2",22,"男");
		Student student3 = new Student("张三3",22,"男");
		Student student4 = new Student("张三4",22,"男");
		list.add(student1);
		list.add(student2);
		list.add(student3);
		list.add(student4);
		ServerResponse serverResponse = ServerResponse.createSuccess("成功",list);
		return serverResponse;
	}
	@RequestMapping("/deleteAll")
	public void deleteAll(Integer[]ids,HttpServletRequest request) {
		System.out.println("StudentController.deleteAll()");
		String[]idArray = request.getParameterValues("ids");
		System.out.println(Arrays.toString(idArray));
		System.out.println(Arrays.toString(ids));
	}
	@RequestMapping("/checkName")
	@ResponseBody
	public boolean checkName() {
		boolean isExist = true;
		return isExist;
	}
	
	@RequestMapping("/checkName1")
	@ResponseBody
	public Map<String, Object> checkName1(){
		Map<String, Object>map= new HashMap<String, Object>();
		boolean isExist = true;
		map.put("isExist",isExist);
		map.put("age",33);
		return map;
	}
}